package com.vrv.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

public class FileUtil {

    private static final Logger logger = Logger.getLogger(FileUtil.class);

    private FileUtil() {
    }

    private static FileUtil fileUtil = null;

    private static String FILE_PATH = "";

    public static FileUtil getInstance(HttpServletRequest request) {
        FILE_PATH = request.getRealPath("/");
        if (fileUtil == null) {
            fileUtil = new FileUtil();
        }
        return fileUtil;
    }

    public void writeFile(String text) {
        // file(内存)----输入流---->【程序】----输出流---->file(内存)
        File file = createFile("");

        // 向文件写入内容(输出流)
        byte bt[] = new byte[1024];
        bt = text.getBytes();
        try {
            FileOutputStream in = new FileOutputStream(file);
            in.write(bt, 0, bt.length);
            in.close();
        } catch (FileNotFoundException e) {
            logger.error("FileNotFoundException:" + e);
        } catch (IOException e) {
            logger.error("IOException:" + e);
        }
    }

    private static File createFile(String dir) {
        // file(内存)----输入流---->【程序】----输出流---->file(内存)
        File file = null;
        file = new File(dir, "dbinfo.properties");
        if (!file.exists()) {
            file.mkdir(); // 创建文件
        }
        return file;
    }

    public static void main(String[] args) {
        createFile("/home/a/");
    }
}
